// pages/site/site.js
var app = getApp();
var server = require('../../utils/server');
const util = require("../../utils/util.js")
const home = require("../home/home.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: '请输入搜索内容',
    searchWords:"",
    siteList:[{
      "title":"郑州站",
      "text":"二马路"
    }, {
      "title": "郑州站",
      "text": "二马路"
    }]
  },
  inputSearch: function (e) {
    var Name = e.detail.value;    
    this.GetAddressByVagueName(Name); //模糊查询
    this.setData({
      searchWords: Name
    });
  },
  doSearch: function () {
    var Name = this.data.searchWords;
    this.GetAddressByName(Name);
    this.setData({
      showResult: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.GetAddress(0);
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  RePosition:function(){  //重新定位
    this.GetAddress(1);
  },
  GetAddress: function (types) {  //获取地理位置  types判断查询类型：0、从本地获取位置 1、重新获取定位信息 
    util.ShowLoading('获取中...');
    var that = this;
    if(types==0){
      wx.getStorage({
        key: 'AddressInfo',
        success: function (res) {
          that.setData({ Address: res.data.address, Recommend: res.data.formatted_addresses.recommend, City: res.data.address_component.city,Location:res.data.location});
          wx.hideLoading();
        }
      });

    }else{
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          var latitude = res.latitude;
          var longitude = res.longitude;
          util.GetAddressByPosition(latitude, longitude, function (e) {
            var recommend = e.result.formatted_addresses.recommend;
            var address = e.result.address;
            var city = e.result.address_component.city;
            var location = e.result.location;
            that.setData({ Address: address, Recommend: recommend,City:city,Location:location });
            wx.hideLoading();
          });
        }
      });
    }    
  },
  GetAddressByVagueName:function(name){  //模糊查询地址信息
    var that = this;
    var Name = name;
    var City = that.data.City;
    util.GetAddressByNameAndCity(Name, City, function (e) {
      if(e.count>0){
        that.setData({ siteList: e.data });
      }else{

      }      
    });
  },
  GetAddressByName: function (name) {  //模糊查询地址信息
    util.ShowLoading('搜索中...');
    var that = this;
    var Name = name;
    var City = that.data.City;
    Name = City+Name;
    util.GetPositionByName(Name, function (da) {  //根据经纬度获取到信息
      var latitude = da.result.location.lat;
      var longitude = da.result.location.lng;
      util.GetAddressByPosition(latitude, longitude, function (e) {
        if(e.status==0){  //搜索成功
          var title = e.result.formatted_addresses.recommend;
          var address = e.result.address;   
          var lat = e.result.location.lat;  //纬度
          var lng = e.result.location.lng;  //经度
          that.setData({ siteList: [{ "title": title, "address": address, "lat": lat,"lng":lng}]});
          //that.setData({ Address: address, Recommend: recommend, City: city });
        } else {  //搜索失败


        }
        wx.hideLoading();
      });
    });
  },
  CheckPosition : function(e){  //选择地理位置       
    var address = e.currentTarget.dataset.title;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({ Address: "" });
    prevPage.setData({ Address: address });    
    var data={};
    data.latitude = e.currentTarget.dataset.location.lat;
    data.longitude = e.currentTarget.dataset.location.lng;  
    prevPage.AddressInfo(data);    
    wx.navigateBack({
      delta: 2
    })
  }

})